package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String number, String beginTime,String endTime){
        //分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        LocalDateTime beginTime1=null,endTime1=null;
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(beginTime!=null&&endTime!=null){
            beginTime1 = LocalDateTime.parse(beginTime,df);
            endTime1 = LocalDateTime.parse(endTime,df);}
        //根据订单号模糊查询
        queryWrapper.like(number != null, Orders::getNumber,number);
        queryWrapper.like(number!=null,Orders::getNumber,number);
        queryWrapper.between(beginTime1!=null&&endTime1!=null,Orders::getOrderTime,beginTime1,endTime1);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = records.stream().map((item) -> {
            OrdersDto orderDto = new OrdersDto();
            //对象拷贝
            BeanUtils.copyProperties(item, orderDto);
            //用户id
            Long userId = item.getUserId();
            //根据UserId查询User对象
            User user = userService.getById(userId);
            if (user != null){
                //UserName
                String userName = user.getName();
                orderDto.setUserName(userName);
            }
            return orderDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(pageInfo);
    }
//    @GetMapping("/page")
//    public R<Page<Orders>> page(int page, int pageSize, Long number, String beginTime,String endTime){
//        LocalDateTime beginTime1=null,endTime1=null;
//        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        if(beginTime!=null&&endTime!=null){
//            beginTime1 = LocalDateTime.parse(beginTime,df);
//            endTime1 = LocalDateTime.parse(endTime,df);}
//        Page<Orders> ordersPage=new Page<>(page,pageSize);
//        LambdaQueryWrapper<Orders> wrapper=new LambdaQueryWrapper<>();
//        wrapper.like(number!=null,Orders::getNumber,number);
//        wrapper.between(beginTime1!=null&&endTime1!=null,Orders::getOrderTime,beginTime1,endTime1);
//        wrapper.orderByDesc(Orders::getOrderTime);
//        ordersService.page(ordersPage,wrapper);
//        return R.success(ordersPage);
//    }
    //http://localhost:8080/order/userPage?page=1&pageSize=1
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize, HttpSession httpSession){
        //创建分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Long userId = (Long) httpSession.getAttribute("user");
        //创建查询条件构造器
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Orders::getUserId,userId);
        ordersService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据"+orders.toString());
        ordersService.submit(orders);
        return R.success("下单成功");
    }
    @PutMapping
    public R<String> changStatus(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(Orders::getId,orders.getId());
        wrapper.set(Orders::getStatus,orders.getStatus());
        ordersService.update(wrapper);
        return R.success("更改状态成功");
    }
}
